package org.ddr.member.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

public abstract class MemAbstractExecutor {
	protected Connection con;
	protected PreparedStatement pstmt;
	protected ResultSet rs;
	Logger log = Logger.getLogger("Log");
	
	static {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Class.forName("org.apache.commons.dbcp.PoolingDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public final void doJob() throws Exception{
		try {
			log.info("----------------1------------------");
			ready();
			
			log.info("----------------2------------------");
			con.setAutoCommit(false);
			
			execute();			
			log.info("----------------3------------------");
			
			con.commit();
			log.info("----------------4------------------");
		} catch (Exception e) {
			e.printStackTrace();
			con.rollback();
			throw new Exception(e.getMessage());
			
		}finally {
			log.info("----------------5------------------");
			closeAll();
			
		}
	}
	
	private void ready() throws Exception {
		
		con = DriverManager.getConnection("jdbc:apache:commons:dbcp:pool");
		
	}
	
	protected abstract void execute() throws Exception;
	
	private void closeAll() {
		if (rs != null) { try {rs.close();} catch (Exception e) {;}}
		if (pstmt != null) { try {pstmt.close();} catch (Exception e) {;}}
		if (con != null) { try {con.close();} catch (Exception e) {;}}
	}
}
